package com.aode.music.view.fragment.chatfragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aode.common.bean.request.ChatData;
import com.aode.common.bean.request.Request;
import com.aode.common.bean.response.News;
import com.aode.music.R;
import com.aode.music.adapter.chat.ChatAdapter;
import com.aode.music.databinding.FragmentFriendBinding;
import com.aode.music.viewmodel.FriendViewModel;
import com.example.chat.view.activity.ChatActivity;

import java.util.ArrayList;
import java.util.List;

public class FriendFragment extends Fragment  implements ChatAdapter.ChatInterface{
    private FragmentFriendBinding friendBinding;
    private RecyclerView recyclerView;
    private FriendViewModel friendViewModel;
    private ChatAdapter chatAdapter;
    private List<News> newsList = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        friendBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_friend,container,false);
        friendViewModel = ViewModelProviders.of(this).get(FriendViewModel.class);
        initData();
        initListener();
        return friendBinding.getRoot();
    }

    private void initListener() {
        initFriendListener();
    }

    private void initFriendListener() {
        friendViewModel.getNewsLiveData().observe(getViewLifecycleOwner(),news -> {
            if(news!=null){
                newsList = news;
                initRecyclerView((ArrayList<News>) newsList);
            }
        });
    }

    private void initRecyclerView(ArrayList<News> newsList) {
        chatAdapter = new ChatAdapter(getContext(),newsList,this);
        friendBinding.friendRecycler.setLayoutManager(new LinearLayoutManager(this.getContext(),LinearLayoutManager.VERTICAL,false));
        friendBinding.friendRecycler.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL));
        friendBinding.friendRecycler.addItemDecoration(new DividerItemDecoration(requireContext(),DividerItemDecoration.HORIZONTAL));
        friendBinding.friendRecycler.setAdapter(chatAdapter);
    }

    private void initData() {
        ChatData chatData = new ChatData();
        chatData.setUserId("1");
        friendViewModel.getNews(chatData);
    }

    @Override
    public void onItemClick(News news) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable("news",news);
        intent.putExtras(bundle);
        intent.setClass(getActivity(), ChatActivity.class);
        startActivityForResult(intent, Request.FRIEND_TO_CHAT);
        Log.e("TAG还未跳转",news.toString());
    }
}
